<template>
  <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="110px">
      <el-form-item label="所属群组" prop="groupId">
        <el-select
          v-model="form.groupId"
          filterable
          reserve-keyword
          placeholder="请选择所属群组名字"
          @change="selectTitle"
          :loading="loadingTitle">
          <el-option
            v-for="item in groupList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="发布机器人" prop="botId">
        <el-select
          v-model="form.botId"
          filterable
          reserve-keyword
          placeholder="请选择发布机器人"
          :loading="loadingBot">
          <el-option
            v-for="item in botList"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标题" prop="title">
        <el-input v-model="form.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="msg">
        <el-input v-model="form.msg" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="发送时间" prop="sendTime">
        <el-date-picker
          v-model="form.sendTime"
          type="datetime"
          placeholder="选择日期时间"
          align="right"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否重试" prop="sendRetry">
        <el-radio v-model="form.sendRetry" :label=true>是</el-radio>
        <el-radio v-model="form.sendRetry" :label=false>否</el-radio>
      </el-form-item>
      <el-form-item label="是否顶置消息" prop="topType">
        <el-radio v-model="form.topType" :label=true>是</el-radio>
        <el-radio v-model="form.topType" :label=false>否</el-radio>
      </el-form-item>
      <el-form-item v-if="form.topType" label="顶置时长(分钟)" prop="effectiveTime">
        <el-input v-model="form.effectiveTime" placeholder="是否顶置消息"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {add, getGroupList, getOneById, update} from "@/api/bot/groupmsg";
import {getBotByGroupId} from "@/api/bot/bot";

export default {
  data() {
    return {
      // 遮罩层
      loading: true,
      groupList: {},
      botList: {},
      loadingTitle: false,
      loadingBot: false,
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单校验
      rules: {
        groupId: [
          {required: true, message: "群组不能不空", trigger: "blur"},
        ],
        botId: [
          {required: true, message: "机器人不能不空", trigger: "blur"},
        ],
        title: [
          {required: true, message: "标题不能不空", trigger: "blur"},
        ],
        msg: [
          {required: true, message: "内容不能不空", trigger: "blur"},
        ],
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '名天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周后',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() + 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },
    }
  },
  methods: {
    initFun(id) {
      this.getGroup()
      this.open = true;
      if (id) {
        getOneById(id).then((response) => {
          this.title = "修改机器人信息";
          this.selectTitle(response.groupId)
          this.form = response;
        });
      } else {
        this.reset()
        this.title = "添加群消息";

      }

    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        groupId: undefined,
        botId: undefined,
        title: undefined,
        msg: undefined,
        sendTime: undefined,
        sendRetry: true,
        topType: false,
        sendStatus: undefined,
        effectiveTime: undefined,
        createdTime: undefined,
      };
      this.resetForm("form");
    },
    getGroup() {
      getGroupList().then(res => {
        this.groupList = res
        this.loadingTitle = false
      })
    },
    selectTitle(row) {
      getBotByGroupId(row).then(res => {
        this.botList = res
        this.loadingBot = false
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            update(this.form).then(response => {
              if (response === "OK") {
                this.msgSuccess("修改成功");
                //关闭弹框
                this.open = false;
                this.$emit("close")
              }
            });
          } else {
            add(this.form).then(response => {
              if (response === "OK") {
                this.msgSuccess("新增成功");
                this.open = false;
                this.$emit("close")
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
  }
}
</script>


<style scoped lang="scss">

</style>
